package cn.edu.cug.cs.gtl.gsm.redis;

import cn.edu.cug.cs.gtl.common.Identifier;
import cn.edu.cug.cs.gtl.gsm.PaginationStorageManager;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import redis.clients.jedis.Jedis;

import java.io.IOException;

/**
 * @Author: jitao
 * @Date: 2022-07-24-10:07
 * @Description:
 */
public class RedisStorageManager extends PaginationStorageManager {
    Jedis jedis;

    public RedisStorageManager(String ip, int port, int databaseName, String baseName, long collectionSize, int pageSize, boolean overwrite) {
        super(baseName, collectionSize, pageSize);
        this.jedis = new Jedis(ip, port);
        this.jedis.select(databaseName);
        boolean exist = jedis.exists("index");
        if (overwrite) {
            this.jedis.flushDB();
        } else {
            if (exist) {
                try{
                    readMetaFromStorage();
                    readIndicesFromStorage();
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    protected void readBufferFromPage(Identifier pageIdentifier) throws IOException {
        int order = this.metaItem.getCollectionOrder(pageIdentifier);
        String dataCollectionName = this.metaItem.getBaseName() + "_"+String.valueOf(order).toString();
        //需要进行集合切换
        if(order!=getCurrentCollectionOrder()){
            setCurrentCollectionOrder(order);
        }
        String sid = pageIdentifier.toString();
        byte[] bs = this.jedis.hget(dataCollectionName.getBytes(), sid.getBytes());
        System.arraycopy(bs,0,this.pageBuffer,0,Math.min(bs.length,getPageSize()));

    }

    @Override
    protected void writeBufferToPage(Identifier pageIdentifier) throws IOException {
        int order = this.metaItem.getCollectionOrder(pageIdentifier);
        String dataCollectionName = this.metaItem.getBaseName() + "_"+String.valueOf(order).toString();
        //需要进行集合切换
        if(order!=getCurrentCollectionOrder()){
            setCurrentCollectionOrder(order);
        }
        String sid = pageIdentifier.toString();
        this.jedis.hset(dataCollectionName.getBytes(), sid.getBytes(), this.pageBuffer);
    }

    @Override
    protected void readMetaFromStorage() throws IOException {
        byte[] bs = this.jedis.get("meta".getBytes());
        this.metaItem.loadFromByteArray(bs);
    }

    @Override
    protected void writeMetaToStorage() throws IOException {
        byte[] bs = this.metaItem.storeToByteArray();
        this.jedis.set("meta".getBytes(), bs);
    }

    @Override
    protected void readIndicesFromStorage() throws IOException {
        byte[] bs = this.jedis.get("index".getBytes());
        this.pageMap.loadFromByteArray(bs);
    }

    @Override
    protected void writeIndicesToStorage() throws IOException {
        byte[] bs = this.pageMap.storeToByteArray();
        this.jedis.set("index".getBytes(), bs);
    }

    @Override
    protected void closeStorages() throws IOException {
        this.jedis.close();
    }
}
